﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace XLDL.Admin
{
    public partial class Banner : System.Web.UI.UserControl
    {
        string path = System.Configuration.ConfigurationManager.AppSettings["Images_Path"];
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DropDownList_hienthi.SelectedIndex = 1;
                xuat_luoi();
            }
        }
        protected void xuat_luoi()
        {
            DataTable tb = new DataTable();
            tb = XLDL.XLDaTa("select * from Banner order by ngay_dang DESC");
            Luoi.DataSource = tb;
            Luoi.DataBind();
            lbl_thongke.Text = "Tổng số Banner: " + tb.Rows.Count.ToString();
        }

        protected void Button_them_Click(object sender, EventArgs e)
        {
            DataTable tb = new DataTable();
            if (FileUpload_hinhanh.HasFile)
            {
                SaveFile(FileUpload_hinhanh);
                string lenh = "insert into banner(hinh_anh,link,hien_thi,ngay_dang) values('" + this.lbl_hinhanh.Text + "','" + this.txt_link.Text.Trim() + "','" + DropDownList_hienthi.SelectedItem.ToString() + "','" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "')";
                tb = XLDL.XLDaTa(lenh);
                reset();
                xuat_luoi();
            }
        }
       


        protected void Button_reset_Click(object sender, EventArgs e)
        {
           // reset();
          //  Button_up.Enabled = true;
           // Button_up.Text = "...";
        }
        protected void reset()
        {
            //this.FileUpload_hinhanh.Visible =false;
            //lbl_hinhanh.Text = "";
            //DropDownList_hienthi.SelectedIndex = 1;
            //txt_id.Text = "";
            
        }
        protected void Luoi_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
           // this.FileUpload_hinhanh.Visible =false;
            Label lbl_id = (Label)Luoi.Rows[e.NewSelectedIndex].Cells[1].FindControl("lbl_id");
            txt_id.Text = lbl_id.Text;
            // tim hinh_anh
           Image hinh_anh =(Image) Luoi.Rows[e.NewSelectedIndex].Cells[2].FindControl("hinh_anh");
           lbl_hinhanh.Text = hinh_anh.AlternateText;

           txt_link.Text = Luoi.Rows[e.NewSelectedIndex].Cells[3].Text;
           if (txt_link.Text == "&nbsp;") { txt_link.Text = txt_link.Text.Remove(0); }
           string s = Luoi.Rows[e.NewSelectedIndex].Cells[4].Text;
         //  this.Button_up.Text = "...";
         //  this.Button_up.Enabled = true;
           if (s == "True")
           {
               this.DropDownList_hienthi.SelectedIndex = 0;
           }
           else {
               this.DropDownList_hienthi.SelectedIndex = 1;
           }
        }

        protected void Button_up_Click(object sender, EventArgs e)
        {
          //  this.PreRender += new EventHandler(FileUpload_hinhanh_PreRender);
           
         //   this.Button_sua.Text = "=";
        //    setButton_up(this.Button_up);
                        
        }
        protected void setButton_up(Button bt)
        {
            if (bt.Text == "...")
            {
                this.FileUpload_hinhanh.Visible = true;
                bt.Text = "=";
                bt.Enabled = false;
                lbl_hinhanh.Text = "";
                DropDownList_hienthi.SelectedIndex = 1;
            }
        }

        protected void SaveFile(FileUpload fu)
        {
                fu.SaveAs(Server.MapPath(path + fu.FileName));
                lbl_hinhanh.Text = fu.FileName;
              //  this.Button_up.Text = "...";
             //   this.FileUpload_hinhanh.Visible = false;
             //   this.Button_up.Enabled = true;
           
           
        }

      //  protected void FileUpload_hinhanh_PreRender(object sender, EventArgs e)
        //{
        //    lbl_hinhanh.Text = FileUpload_hinhanh.FileName;
        //}

        protected void Button_sua_Click(object sender, EventArgs e)
        {
            DataTable tb = new DataTable();
            string lenh = "update Banner set hien_thi='" + DropDownList_hienthi.SelectedItem.ToString()+ "', link='"+txt_link.Text.Trim()+"' ";
            if (FileUpload_hinhanh.HasFile)
            {
                if (FileUpload_hinhanh.FileName != lbl_hinhanh.Text)
                {
                   
                    System.IO.File.Delete(Server.MapPath(path + lbl_hinhanh.Text));
                    lenh += ", hinh_anh='" + FileUpload_hinhanh.FileName + "'";
                    SaveFile(FileUpload_hinhanh);
                }
                
            }
            lenh+=" where id_banner=" + int.Parse(txt_id.Text) + "";
            tb = XLDL.XLDaTa(lenh);
            reset();
            xuat_luoi();

        }

        protected void Button_xoa_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable tb = new DataTable();
                tb = XLDL.XLDaTa("delete from Banner where id_banner=" + int.Parse(txt_id.Text) + "");
                System.IO.File.Delete(Server.MapPath(path + lbl_hinhanh.Text));
                reset();
                xuat_luoi();
                
            }
            catch { }
        }

      

      

       
    }
       
}